<%@ page  pageEncoding="utf-8"%>
<%@page import="org.myhibernate.Template"%>
<%@page import="org.myhibernate.ProxyTemplate"%>
<%@page import="org.myhibernate.core.method.PageSizeQuery"%>
<%@page import="java.util.List"%>
<%@page import="org.json.JSONArray"%>
<%@page import="org.json.JSONObject"%>
<%@page import="com.util.ConfigUtil"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Enumeration"%>
<%@page import="org.personal.entity.Page"%>
<%@page import="org.personal.entity.Layout"%>
<%@page import="org.personal.entity.User"%>
<%@page import="org.personal.Constants"%>
<%@page import="org.personal.entity.UserPage"%>
<%@page import="org.personal.util.UserUtil"%>
<%@page import="org.personal.entity.Portlet"%>
<%

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String pageId=request.getParameter("pageId");
Page pageObject=null;
UserPage userPage=null;
List portlets=null;
User user=(User)request.getSession().getAttribute(Constants.USER);
StringBuffer ids=new StringBuffer(";");
int layoutId=-1;
String layoutTitle="";

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>我的页面管理</title> 
<%@include file="../styles/styles.jsp" %>
<script type="text/javascript" src="<%=basePath%>js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="<%=basePath%>js/jquery.showPopupWindow.js"></script>
<script type="text/javascript" src="<%=basePath%>js/getBrowserInfo.js"></script>

  </head>
  <body>
    <table cellspacing="0" cellpadding="0" class="tab" id="userform" style="width:100%;display:;"  >
	<tr>
	<td>&nbsp; 
	
	<input type="button"   class="mybutton" value="添加板块" onclick="buttonAction('add')" style="" />
	<input type="button"   class="mybutton" value="切换布局" onclick="buttonAction('layout')" style="" />
	&nbsp;&nbsp;
	当前布局:<span id="currentLayout"></span>
	&nbsp;&nbsp;
	<span id="message" ></span>
	</td></tr>
	</table>
	<br/>
	
     <div id="listtable" >
  <table cellspacing="0" cellpadding="0" class="tab"  style="width:100%;display:;"  >
  <thead>
	<tr><td>名称</td><td>操作</td></tr></thead>
<%
	String pagePath="../index.jsp";
	if (ConfigUtil.getAttribute(Constants.DEFAULT_PAGE)!=null)
	{
		pagePath="../"+ConfigUtil.getAttribute(Constants.DEFAULT_PAGE);
	}
	//有用户
	if(user!=null)
	{
		Template template_page=new ProxyTemplate(Page.class).getInstance();
	   	Template template_portlet=new ProxyTemplate(Portlet.class).getInstance();
	   	Template template_userPage=new ProxyTemplate(UserPage.class).getInstance();
	   	Template template_layout=new ProxyTemplate(Layout.class).getInstance();
	   	
	   	pageObject=(Page)template_page.get(Integer.parseInt(pageId));
	   	//有页面
	   	if(pageObject != null)
	   	{
			UserUtil userUtil=new UserUtil();
			portlets=pageObject.getPortlets();
			layoutId=pageObject.getLayout();
			
			boolean editable=false;
			editable=userUtil.hasPageEdit(request);
			//有编辑权限
			if(editable)
			{
				PageSizeQuery<UserPage> query_userPage=template_userPage.getPageSizeQuery();
				int count=query_userPage.query(" where userId='"+user.getUid()+"' and pageId="+pageId+" ");
				//有个性化配置
				if (count>0)
				{
					userPage=query_userPage.getListObject(0, 1).get(0);
					portlets=userPage.getPortlets();
					layoutId=userPage.getLayoutId(); 
				}
			}else
			{
				//System.out.println("没有编辑权限");
				response.sendRedirect(pagePath);	
			}
	   	}else
	   	{
	   		//System.out.println("页面不存在");
	   		response.sendRedirect(pagePath);	
	   	}//endif pageObject!=null
	   	
	   	Layout layout=null;
	   	if((layoutId!= -1))
	   	{
	   		layout=(Layout)template_layout.get(layoutId);
				if(layout!=null)
				{
					layoutTitle=layout.getTitle();	
				}
	   	}
	   	if(layout==null)
		{
			PageSizeQuery queryLayout=template_layout.getPageSizeQuery();
			int count=queryLayout.query(" where defaultLayout='true' ");
			if(count>0)
			{
				layout=(Layout)queryLayout.getListObject(0,1).get(0);
				layoutTitle=layout.getTitle();	
				layoutId=layout.getId();
			}
					
		}
				
	   	// portlets=[2:0:2, 2:1:3]
	  // 	System.out.print("portlets="+portlets);
	   	try
	   	{
	   			if((portlets != null)&&(portlets.size()>0))
			   	{
			   		for(int i=0;i<portlets.size();i++)
			   		{
			   			String portletInfo=portlets.get(i)+"";
			   			String portletId=portletInfo.split(":")[2];
			   			Portlet portlet=(Portlet)template_portlet.get(portletId);
			   			if(portlet != null)
			   			{
			   				ids.append(portletId+";");
			   				%>
							<tr>
			    				<td><%=portlet.getTitle()%></td>
								<td>
			    					<a href="javascript:;" onclick="actionFunction('remove','<%=portlet.getId()%>','<%=portlet.getTitle()%>')">移除</a>
			    				</td>
			    				</tr>   			
			   			<%
			   			}else
			   			{
			   				%>
							<tr>
			    				<td><font color=red>未知名称板块</font></td>
								<td>
			    					<a href="javascript:;" onclick="actionFunction('remove','<%=portletId%>','未知名称板块')">移除</a>
			    				</td>
			    				</tr>   			
			   			<%
			   			}
			   		}
			   	}else
			   	{
			   		out.println("<tr><td colspan=2 align=center >暂无数据</td></tr>");
			   	}
	   	}catch(Exception exception){}
	   	finally
	   	{
	   		template_page.close();
	   		template_portlet.close();
	   		template_userPage.close();
	   		template_layout.close();
	   	}	
	   
	}else
	{
		//System.out.println("用户未登录");
		response.sendRedirect(pagePath);	
	}//endif user!=null
	
	
   	
%>
</table>
<div id="pagenav"></div>
   </div>
   <br/>
   
 
<script>
var map=new Object();
function get(id){return document.getElementById(id);}
var message=get("message");
var ids="<%=ids.toString()%>";
var layoutId="<%=layoutId%>";
get("currentLayout").innerHTML="<%=layoutTitle%>";
function actionFunction(type,id,title)
{
	 if("remove" == type)
	{
		if(confirm("确定移除系统？"+title))
		{
			message.innerHTML='<img src="<%=basePath%>images/loading.gif" border=0 />';
			var args="id="+id+"&pageId=<%=pageId%>";
			new doCallRequest("<%=basePath%>action/deleteMyPortlet.jsp",args,function(data)
			{
				message.innerHTML='';
				var json=toJson(data);
				//alert("data="+data);
				if("ok" == json.result)
				{
					window.location.reload();
				}else
				{
					message.innerHTML=json.message;
				}
				
			});
		}
	}
}

function buttonAction(type)
{
	if("add" == type)
	{
		showPopupWindow(600,400,"<%=basePath%>user/choosePortlet.jsp","","","添加板块",true);
	}else if("layout" == type)
	{
		showPopupWindow(600,400,"<%=basePath%>user/chooseLayout.jsp","","","选择布局",true);
	}
}
function addPortlet(id)
{
	closeShowPopupWindow();
	if(ids.indexOf(";"+id+";") >= 0)
	{
		alert("选择的板块已经存在");
		return;
	}
	message.innerHTML='<img src="<%=basePath%>images/loading.gif" border=0 />';
	new doCallRequest("<%=basePath%>action/addPortlet.jsp","id="+id+"&pageId=<%=pageId%>",function(data)
	{
		var json=toJson(data);
		message.innerHTML="";
		if("ok" == json.result)
		{
			window.location.reload();
		}else
		{
			message.innerHTML=json.message;
		}
				
	});
}
function chooseLayout(id)
{
	closeShowPopupWindow();
	//alert("addPortlet id="+id);
	if(layoutId == id)
	{
		alert("当前布局已经应用");
		return;
	}
	message.innerHTML='<img src="<%=basePath%>images/loading.gif" border=0 />';
	new doCallRequest("<%=basePath%>action/setLayout.jsp","id="+id+"&pageId=<%=pageId%>",function(data)
	{
		var json=toJson(data);
		message.innerHTML="";
		if("ok" == json.result)
		{
			window.location.reload();
		}else
		{
			message.innerHTML=json.message;
		}
				
	});
}
</script>
  </body>
</html>
